"""
自定义定义返回的报文

{
    "status": "failure",
    "code": 20001,
    "message": "",
    "data": [],
    "errors": [],
    "timestamp": "2021-12-17T05:53:21+00:00"
}
"""
import logging
import datetime
from rest_framework.response import Response

logger = logging.getLogger('django')


class SuccessResponse(Response):

    def __init__(self, data=None, message='success', status=200, template_name=None, headers=None, exception=False,
                 content_type=None):
        self.std_data = {
            "status": 'success',
            "code": status,
            "data": data,
            "msg": message,
            "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:%f")
        }
        super().__init__(self.std_data, status, template_name, headers, exception, content_type)

    def __str__(self):
        return str(self.std_data)


class ErrorResponse(Response):

    def __init__(self, data=None, msg='error', code=201, status=None, template_name=None, headers=None,
                 exception=False, content_type=None):
        # 记录真实的异常信息
        logger.error(" ")
        logger.error("################    Error    #################")
        logger.error(" ")
        logger.error(msg)
        self.std_data = {
            "status": 'error',
            "code": code,
            "data": data,
            # "msg": "系统服务异常！！",
            "msg": msg,
            "timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:%f")
        }
        logger.error(msg)
        super().__init__(self.std_data, status, template_name, headers, exception, content_type)

    def __str__(self):
        return str(self.std_data)
